import { IS_BUN_RUNTIME } from '../cli/utils/runtime-utils.js';
import { logger } from './logger.js';
import { readFileSync, existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import p from 'node:path';

let g=[];class c{_variables;static _data;constructor(e){this._variables=e;}get(e){const r=e.split(".");let n=this._variables;for(const i of r)if(typeof n=="object"&&n!==null&&i in n)n=n[i];else throw new Error(`Invalid key path: ${e}`);if(typeof n=="object"&&n!==null&&"env"in n){const i=n.env,t=process.env[i];return t!==void 0?t:"default"in n&&n.default!==void 0?n.default:void 0}throw new Error(`Invalid schema configuration for key: ${e}`)}static data(){return this._data}static async load(e={}){const r=b(e);if(r){const n=await readFile(r,"utf-8"),i=y(n);return c._data=i,h(e,i)}else return {}}static loadSync(e={}){const r=b(e);if(r){const n=readFileSync(r,"utf-8"),i=y(n);return c._data=i,h(e,i)}else return {}}}const R=new c({discord:{clientId:{env:"DISCORD_CLIENT_ID"},debugChannelId:{env:"DISCORD_DEBUG_CHANNEL_ID"},guildId:{env:"DISCORD_GUILD_ID"},token:{env:"DISCORD_TOKEN"}},nodeEnv:{env:"NODE_ENV"},roboplay:{api:{env:"ROBOPLAY_API",default:"https://api.roboplay.dev"},debug:{env:"ROBOPLAY_DEBUG"},env:{env:"ROBOPLAY_ENV"},frontend:{env:"ROBOPLAY_FRONTEND",default:"https://roboplay.dev"}}});function h(o,e){const{overwrite:r=g}=o,n=/\${(.+?)}/g,i={...process.env};try{for(const t in e){if(!(r===!0||Array.isArray(r)&&r.includes(t))&&t in i)continue;const l=new Set;let a=e[t];for(;n.test(a);)a=a.replace(n,(f,s)=>{if(l.has(s))throw new Error(`Circular reference detected in environment variable "${t}"`);return l.add(s),i[s]||e[s]||""});i[t]=a;}Object.assign(process.env,i);}catch(t){logger.error("Could not load environment variables:",t);}return e}function b(o){if(IS_BUN_RUNTIME)return null;const{mode:e}=o;let{path:r=p.join(process.cwd(),".env")}=o;if(e&&existsSync(r+"."+e)&&(logger.debug("Found .env file for mode:",e,":",r+"."+e),r=p.join(process.cwd(),".env."+e)),!existsSync(r)){logger.debug(`No .env file found at "${r}"`);return}return r}function y(o){const e=o.split(`
`),r=/^\s*#/,n=/^['"]/,i=/\\(.)/g;let t="";const d={};for(let l=0;l<e.length;l++){if(t+=e[l],r.test(t)){t="";continue}if(t.endsWith("\\")){t=t.slice(0,-1);continue}const a=t.indexOf("=");if(a===-1){t="";continue}const f=t.substring(0,a).trim();let s=t.substring(a+1).trim();n.test(s)&&(s=s.slice(1,-1).replace(i,"$1")),d[f]=s,t="";}return d}function D(o){g=o;}

export { c as Env, R as env, D as setGlobalOverwrites };
//# sourceMappingURL=out.js.map
//# sourceMappingURL=env.js.map